import api from "../../api/index.js";
Page({
  data: {
    currentBalance: null, // 当前余额
    monthFilter: '', // 月份筛选
    typeIndex: 0, // 类型筛选索引
    typeFilters: [
      { label: '全部类型', value: 'all' },
      { label: '充值', value: 'recharge' },
      { label: '消费', value: 'expense' }
    ],
    logs: [], // 日志列表
    user:{}
  },

  onLoad() {
    // 初始化加载数据
    this.loadLogs();
    let user = wx.getStorageSync('user')
    this.setData({
      user
    })
    // 设置默认月份为当前月
    const now = new Date();
    this.setData({
      monthFilter: `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}`
    });
    //获取会员信息
    api.memberCardInfo({
      method: "GET",
      id: this.data.user.memberCardId
    }).then((res) => {
      this.setData({
        currentBalance :res.data.balance
      })
    })
  },

  // 加载日志数据
  loadLogs() {
    api.memberCardBalanceLog({data:{}})
    .then((res)=>{
      this.setData({
         logs: res.data ,
        });
    })
  },

  // 月份筛选变化
  onMonthChange(e) {
    this.setData({
      monthFilter: e.detail.value,
    });
    this.loadLogs();
  },

  // 类型筛选变化
  onTypeChange(e) {
    this.setData({
      typeIndex: e.detail.value,
    });
    this.loadLogs();
  }
});